package com.sy.common.crud;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.sy.common.bean.PageBean;
import com.sy.common.utils.BeanOfMap;
import org.springframework.beans.factory.annotation.Autowired;

import java.io.Serializable;
import java.util.List;
import java.util.Map;

public abstract class BaseServiceImpl<T, M extends IMapper> implements IService<T> {
    @Autowired(required = false)
   protected M baseMapper;

    @Override
    public int save(T entity) {
        return baseMapper.save(entity);
    }

    @Override
    public int update(T entity) {
        return baseMapper.update(entity);
    }

    @Override
    public int delete(Serializable id) {
        return baseMapper.delete(id);
    }

    @Override
    public T get(Serializable id) {
        return (T) baseMapper.get(id);
    }

    @Override
    public List<Map> queryList(Map map) {
        return baseMapper.queryList(map);
    }

    @Override
    public PageBean query(Map map) {
        map.putIfAbsent("page", 1);
        map.putIfAbsent("pageSize", 10);
        int page = Integer.parseInt(map.get("page").toString());
        int pageSize = Integer.parseInt(map.get("pageSize").toString());
        if (page==0){
            page=1;
        }
        if (pageSize==0){
            pageSize=10;
        }
        PageHelper.startPage(page, pageSize);
        List list = baseMapper.query(map);
        PageInfo info = new PageInfo(list);
        return new PageBean<>(page, pageSize, info.getTotal(), list);
    }
}
